package demo;

/**
 * @Author liangzai
 * @Description:
 */
public class Training {
    //行相等的最少多米诺旋转
    public int minDominoRotations(int[] tops, int[] bottoms) {
        int[] nums_t = new int[7];
        int[] nums_b = new int[7];
        int len = tops.length, max_t = 0, max_d = 0;
        for(int i=0; i<len; i++){
            nums_t[tops[i]]++;
            nums_b[bottoms[i]]++;
            max_t = Math.max(max_t, nums_t[tops[i]]);
            max_d = Math.max(max_d, nums_b[bottoms[i]]);
        }
        int index_t = 0, index_d = 0;
        for(int i=1; i<7; i++){
            if(max_t == nums_t[i]){
                index_t = i;
            }
            if(max_d == nums_b[i]){
                index_d = i;
            }
        }
        int count_t = 0, count_d = 0;
        for(int i=0; i<len; i++){
            if(tops[i]!= index_t && bottoms[i]==index_t && count_t>=0){
                count_t++;
            }
            else if(tops[i]!=index_t && bottoms[i]!=index_t){
                count_t = -1;
            }
            if(bottoms[i]!=index_d && tops[i]==index_d && count_d>=0){
                count_d++;
            }
            else if(bottoms[i]!=index_d && tops[i]!=index_d){
                count_d = -1;
            }
            if(count_d==-1 && count_t==-1){
                return -1;
            }
        }
        if(count_d>0 && count_t>0){
            return count_d>count_t ? count_t : count_d;
        }
        return count_d>count_t ? count_d : count_t;
    }
}
